<?php
class C_users extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('m_users');
	}
	function index(){
		$task = $this->input->post('task');
		switch($task){
			case 'LIST' :
				$this->getlist();
				break;
			case 'GET_STORE_COMBO' :
				$this->getstorecombo();
				break;
			case 'SAVE' :
				$this->insert();
				break;
			case 'EDIT' :
				$this->edit();
				break;
			case 'DELETE' :
				$this->delete();
				break;
			default :
				$this->load->view('v_users');
		}
	}
	
	function getlist(){
		$filter = $this->input->post('query');
		$limit  = $this->input->post('limit');
		$offset = $this->input->post('start');
		echo $this->m_users->getlist($limit,$offset,$filter); 
	}
	
	function getstorecombo(){
		echo $this->m_users->getstorecombo();
	}
	function insert(){
		$query = $this->db->select('user_id')->where('user_name',$this->input->post('user_name'))->get('s_users');
		if($query->num_rows() > 0){
			echo $message = 0;
		} else {
			$data = array('user_name'   => $this->input->post('user_name'),
						  'user_passwd' => md5($this->input->post('user_passwd')),
						  'user_groups'  => $this->input->post('user_group'));
						  // 'user_aktif'  => 'Y');
			$this->m_users->insert($data);
			echo	$message = 1;
		}
	}
	function edit(){
		$user_id = $this->input->post('user_id'); 		
		$query = $this->db->select('user_id')->where('user_name',$this->input->post('user_name'))->where('user_id <>',$user_id)->get('s_users');
		if($query->num_rows() > 0){
			echo $message = 0;
		} else {
			$data = array('user_name'   => $this->input->post('user_name'),
						  'user_passwd' => md5($this->input->post('user_passwd')),
						  'user_groups' => $this->input->post('user_group'));
			$this->m_users->edit($data,$user_id);	
			echo $message = 1;
		}
		// echo $this->db->last_query();
	}
	function delete(){
		$user_id = $this->input->post('user_id');
		$data    = array('user_aktif '=> 'T');
		$this->db->where('user_id',$user_id)->update('s_users',$data);
		$message = 1;
		echo $message;
	}
}